/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system;

import java.io.IOException;
import java.util.AbstractMap.SimpleEntry;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspWriter;

/**
 *
 * @author Iyus
 */
public class chatbetweenfriend {
    //sistem yang dapat menampung percakapan antara dua friend
    //isinya adalah id, dari friend yang masing-masing memiliki chat masing-masing

    public HashMap<Entry<String, String>, List<pairClass>> chat_system = new HashMap<Entry<String, String>, List<pairClass>>();
    public HashMap<Entry<String, String>, Boolean> isUpdate = new HashMap<Entry<String, String>, Boolean>();
    public void addChatFriend(String user1, String user2) {
        if (!chat_system.containsKey((new SimpleEntry<String, String>(user1, user2))) && !user1.equals(user2)) {
            chat_system.put(new SimpleEntry<String, String>(user1, user2), new LinkedList<pairClass>());
            Boolean put = isUpdate.put(new SimpleEntry<String, String>(user1, user2), Boolean.FALSE);
        }
    }

    public void addChat(String user1, String user2, String message) {
        //cari dulu ada pa ga
        SimpleEntry<String, String> a = new SimpleEntry<String, String>(user1, user2);
        if (chat_system.containsKey(a)) {
            chat_system.get(a).add(new pairClass(user1, message));
            isUpdate.remove(new SimpleEntry<String, String>(user1, user2));
            isUpdate.put(new SimpleEntry<String, String>(user1, user2), Boolean.TRUE);
        } else if(isChatting(user2, user1)){
            a = new SimpleEntry<String, String>(user2, user1);
            chat_system.get(a).add(new pairClass(user1, message));
            isUpdate.remove(new SimpleEntry<String, String>(user2, user1));
            isUpdate.put(new SimpleEntry<String, String>(user2, user1), Boolean.TRUE);
        } else{
            a = new SimpleEntry<String, String>(user1, user2);
            List<pairClass> b = new LinkedList<pairClass>();
            b.add(new pairClass(user1, message));
            chat_system.put(a, b);           
        }
    }

    public boolean isChatting(String user1, String user2) {
        return chat_system.containsKey(new SimpleEntry<String, String>(user1, user2));
    }

    public List<pairClass> getChatContent(String user1, String user2) {
        if (chat_system.containsKey(new SimpleEntry<String, String>(user1, user2))) {
            return chat_system.get(new SimpleEntry<String, String>(user1, user2));
        } else {
            return chat_system.get(new SimpleEntry<String, String>(user2, user1));
        }
    }

    public void printChat(String user1, String user2, JspWriter out) {
        List<pairClass> chat = getChatContent(user1, user2);
        try {
            for (pairClass key : chat) {
                out.println(key.nama + ": " + key.message + "<br/>");
            }
        } catch (IOException ex) {
            Logger.getLogger(chatbetweenfriend.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    //mendapatkan semua user yang sedang chat dengan username
    //intinya ngedapatin semua key dari Entry<String,String> yang valuenya adalah username
    public List<String> getFriendChatWith(String username) {
        List<String> ret = new ArrayList<String>();
        for (Entry<String, String> key : chat_system.keySet()) {
            if (key.getValue().equals(username)) {
                ret.add(key.getKey());
            }
        }
        return ret;
    }

    public void setStatusUpdate(String user1, String user2, boolean update) {
        if (chat_system.containsKey(new SimpleEntry<String, String>(user1, user2))) {
            isUpdate.put(new SimpleEntry<String, String>(user1, user2), update);

        } else {
            isUpdate.put(new SimpleEntry<String, String>(user2, user1), update);

        }
    }

    public boolean getStatusUpdate(String user1, String user2) {
        if (isChatting(user1, user2)) {
            return isUpdate.get(new SimpleEntry<String, String>(user1, user2));
        } else {
            return isUpdate.get(new SimpleEntry<String, String>(user2, user1));
        }
    }

    public boolean isUpdate(String user1, String user2) {
        boolean update = getStatusUpdate(user1, user2);
        if (update) {
            setStatusUpdate(user1, user2, !update);
        }
        return update;
    }

    public void ngejunk() {
        System.out.println("ngejunk");
    }
}
